CUBE CONNECT Edition Help

Looping through the links or nodes with the CubeDatabase class

The links() method allows to retrieve the links of the network in a CubeNetworkEntityResultSet. This is an iterable object where the cursor is set to a record initialized before the first record.

net_links = db.links(network_name)

The cursor “next” will return a Boolean, which is True if the next record exists and False otherwise, and will set the pointer to the next record. A and B nodes can be access directly using the a() and b() functions, and any attribute value can be accessed by using the following methods:

  • integer(), integer64(), real(), string(), return the value of the attribute, in case the format is correctly integers, float or string respectively, otherwise the methods return an “Invalid type for attribute index” error.
  • valueString(), returns the value as a string, regardless of the underlying type of the attribute at the specified index.

Looping through the link records can be done in Python with a while loop, as in the below example.

while net_links.next():
    a_node = net_links.a()
    b_node = net_links.b()
    obj_id = net_links.integer64("objectid")
    speed = net_links.real("speed")
    name = net_links.string("name")

The attributes will be returned with their input type. Otherwise, the below will return a string type value for each attribute value.

while net_links.next():
    obj_id = net_links.valueString("objectid")
    speed = net_links.valueString("speed")
    name = net_links.valueString("name")

A tuple of the attribute names can be obtain using the attributeNames() method for the CubeNetworkEntityResultSet, as below.

link_attr_names = net_links.attributeNames()

The attributes() method instead returns a FieldVector of the attributes for the resultset. It is possible to iterate through the attributes, to retrieve, for example, their name and type, as in the example below.

link_attr = net_links.attributes()
for attribute in link_attr:
    print(attribute.name, attribute.type)

The link resultset can also be converted to a CubeNetworkEntityBuffer, where a buffer represents a section of memory dynamically allocated the link information for all the records of the link resultset. This can be done using the convertToNetworkEntityBuffer method below.

links_buffer = net_links.convertToNetworkEntityBuffer()

The number of entities (links) stored in the buffer are provided by the numEntities method as below.

links_entities = links_buffer.numEntities()

It is then possible to loop through the links using a for loop, obtaining the link attribute values similarly to what seen above, like in the example below.

for link_ix in range(links_entities):
    a_node = links_buffer.a(link_ix)
    b_node = links_buffer.b(link_ix)
    obj_id = links_buffer.integer64(link_ix, "objectid")
    speed = links_buffer.real(link_ix, "speed")
    name = links_buffer.string(link_ix, "name")

Or using valueString, irrespectively of the source type.

for link_ix in range(links_entities):
    obj_id = links_buffer.valueString(link_ix, "objectid")
    speed = links_buffer.valueString(link_ix, "speed")
    name = links_buffer.valueString(link_ix, "name")

Similarly to the link resultset, a tuple with the attribute names, and a FieldVector of them, can be obtained as below.

buff_link_attr_names = link_buffer.attributeNames()
buff_link_attr = link_buffer.attributes()
for attribute in buff_link_attr:
    print(attribute.name, attribute.type)

The same syntax is applied for nodes, using the nodes() method, and convertToNetworkEntityBuffer.

The reader should also notice that additional attributes can be specified for these methods that are not illustrated in the above examples.

The below examples summarizes what illustrated above, with an example of writing to a csv file the link attributes table.

net_links = db.links(network_name)
links_buffer = net_links.convertToNetworkEntityBuffer()  
numb_links = links_buffer.numEntities()
links_attributes = links_buffer.attributes()  # cubeapi.FieldVector

# Open the CSV files to write
with open(output_links_csv, 'w', newline='') as links_csv_file:  
    link_attr_list = ["A", "B"]
# looping through all the attributes elements, within the iterable vector
    for link_attr in links_attributes:
        link_attr_list.append(link_attr.name)
    links_csv_writer = csv.writer(links_csv_file)
    links_csv_writer.writerow(link_attr_list)
# index for the link, looping from 0 to numb_links - 1
    for link_ix in range(numb_links):  
        record = [links_buffer.a(link_ix), links_buffer.b(link_ix)]
# looping through all the attributes elements, within the iterable vector
        for link_attr in links_attributes:  
            record.append(links_buffer.valueString(link_ix, link_attr.name))
        links_csv_writer = csv.writer(links_csv_file)
        links_csv_writer.writerow(record)